package com.jrtc.backend.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.jrtc.base.entity.bo.Role;
import com.jrtc.service.RoleService;
import com.google.common.base.Strings;
import com.jrtc.base.config.dto.ResultDTO;
import com.jrtc.base.config.dto.ResultDTOBuilder;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * 角色表(Role)表控制层
 *
 * @author makejava
 * @since 2022-09-05 18:45:57
 */
@RestController
@RequestMapping("role")
public class RoleController {
    /**
     * 服务对象
     */
    @Resource
    private RoleService roleService;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("selectOne")
    public Role selectOne(Integer id) {
        return this.roleService.queryById(id);
    }
    
    @RequestMapping("getRoleList")
    public ResultDTO getRoleList(@RequestBody Role role)
    {
        IPage<Role> ipage = roleService.queryRoleList(role);
        return ResultDTOBuilder.success(ipage);
    }

    @RequestMapping("insertRole")
    public ResultDTO insertRole(@RequestBody Role role)
    {
        Role tmpRole = roleService.insert(role);
        if (tmpRole != null)
        {
           return ResultDTOBuilder.success("添加成功！");
        }
        return ResultDTOBuilder.failureMsg("添加失败！");
    }

    @RequestMapping("getRoleById")
    public ResultDTO getRoleById(@RequestBody Map<String, String> map)
    {
        String roleId = map.get("roleId");
        Role role = roleService.queryById(Integer.parseInt(roleId));
        if (Strings.isNullOrEmpty(roleId) || role==null) {
            return ResultDTOBuilder.failureMsg("信息有误！");
        }
        return ResultDTOBuilder.success(role);
    }

    @RequestMapping("updateRole")
    public ResultDTO updateRole(@RequestBody Role role)
    {
        if (role==null)
        {
            return ResultDTOBuilder.failureMsg("信息有误！");
        }
        if (roleService.update(role)!=null)
        {
            return ResultDTOBuilder.success("修改信息成功！");
        }
        return ResultDTOBuilder.failureMsg("修改信息失败！");
    }
    
    @RequestMapping("deleteRole")
    public ResultDTO deleteRole(@RequestBody Map<String,String> map)
    {
        String roleId = map.get("roleId");
        if (!Strings.isNullOrEmpty(roleId))
        {
            if (roleService.deleteById(Integer.parseInt(roleId)))
            {
                return ResultDTOBuilder.success("删除成功！");
            }
            return ResultDTOBuilder.failureMsg("删除失败！");
        }
        return ResultDTOBuilder.failureMsg("编号不正确！");
    }

    /**
     * 获取角色列表接口
     * 2022年9月10日
     * */
    @PostMapping("/dirRoleList")
    public ResultDTO dirRoleList(){
        List<Role> roles = roleService.dirRoleList();
        return ResultDTOBuilder.success(roles);
    }
}
